import '@/js/layui_exts/admin.js'
import loginBg from '/public/images/login-bg.jpg'
import loginHeadGif from '/public/images/login-head.gif'
import loginGloble from '/public/images/login-global.png'
import loginForm from '/public/images/login-form.png'
import loginA from '/public/images/login-a.png'
import loginB from '/public/images/login-b.png'
import '@/css/login.css'

layui.use(['jquery', 'admin'], () => {
    const $ = layui.jquery, admin = layui.admin

    Object.entries({
        'body': loginBg,
        '.header .header-img': loginHeadGif,
        '.content .content-left': loginGloble,
        '.content .content-right': loginForm,
        '.right-infp-btn': loginA,
        '.right-infp-name': loginB
    }).forEach(([k, v]) => $(k).css({backgroundImage: 'url(' + v + ')'}))

    let setFontSize = () => $('html').css({fontSize: $(window).width() / 22})

    $(() => setFontSize(), $(window).resize(() => setFontSize()))

    let login = () => {
        $('#login').attr('disabled', true)
        setTimeout(() => $('#login').attr('disabled', false), 1000)

        admin.postM('sys/user/login', {
            username: $('input[name=username]').val(),
            password: $('input[name=password]').val()
        }, d => (admin.setToken(d.data), window.location.href = '/index.html'))
    }

    $('#login').click(() => login()), $('input[name=password]').keydown(e => {
        if (e.keyCode == 13)
            login()
    })
});
